unit uPesquisaCliFor;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls,
  Vcl.Buttons, Data.DB, Vcl.Grids, Vcl.DBGrids;

type
  TfrmPesquisaCliFor = class(TForm)
    rgTipoPesquisa: TRadioGroup;
    PageControl: TPageControl;
    TabSheetClientes: TTabSheet;
    TabSheetFornecedores: TTabSheet;
    GroupBox1: TGroupBox;
    DBGridClientes: TDBGrid;
    DBGridFornecedores: TDBGrid;
    dsTblcli: TDataSource;
    dsTblfor: TDataSource;
    edtPesquisa: TLabeledEdit;
    btnOk: TBitBtn;
    btnCancelar: TBitBtn;
    procedure edtPesquisaChange(Sender: TObject);
    procedure rgTipoPesquisaClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure edtPesquisaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
    procedure Filtrar;
    procedure FiltrarClientes;
    procedure FiltrarProdutos;

  public
    { Public declarations }
  end;

var
  frmPesquisaCliFor: TfrmPesquisaCliFor;

implementation

{$R *.dfm}

uses uConexao, uDadosClientes, uDadosFornecedores;

{ TfrmPesquisaCliFor }

procedure TfrmPesquisaCliFor.edtPesquisaChange(Sender: TObject);
begin
  Filtrar;
end;

procedure TfrmPesquisaCliFor.edtPesquisaKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if rgTipoPesquisa.ItemIndex = 0 then // Pesquisa pro Clientes
  begin
    if not dmDadosClientes.cdsTblcli.Active then
      exit;

    if dmDadosClientes.cdsTblcli.RecordCount = 0 then
      exit;

    if Key = VK_UP then
      dmDadosClientes.cdsTblcli.Prior;

    if Key = VK_DOWN then
      dmDadosClientes.cdsTblcli.Next;
  end
  else
  begin
    if not dmDadosFornecedores.cdsTblfor.Active then
      exit;

    if dmDadosFornecedores.cdsTblfor.RecordCount = 0 then
      exit;

    if Key = VK_UP then
      dmDadosFornecedores.cdsTblfor.Prior;

    if Key = VK_DOWN then
      dmDadosFornecedores.cdsTblfor.Next;
  end;
end;

procedure TfrmPesquisaCliFor.Filtrar;
begin
  if rgTipoPesquisa.ItemIndex = 0 then
  begin
    FiltrarClientes;

    if dmDadosClientes.cdsTblcli.RecordCount > 0 then
      btnOk.Enabled := true
    else
      btnOk.Enabled := false;
  end
  else
  begin
    FiltrarProdutos;

  if dmDadosFornecedores.cdsTblfor.RecordCount > 0 then
    btnOk.Enabled := true
  else
    btnOk.Enabled := false;
  end;
end;

procedure TfrmPesquisaCliFor.FiltrarClientes;
var
  SQL: String;
begin

  if Trim(edtPesquisa.Text) <> EmptyStr then
    SQL := SQL +
      ' and (clicpf like "%' + trim(edtPesquisa.Text) + '%" ' +
      '      or clinome like "%' + trim(edtPesquisa.Text) + '%")';

  SQL :=
    'select *      ' +
    '  from tblcli ' +
    ' where 1 = 1  ' +
    SQL +
    ' order by clinome';

  dmDadosClientes.FiltrarClientes(SQL);

end;

procedure TfrmPesquisaCliFor.FiltrarProdutos;
var
  SQL: String;
begin

  if Trim(edtPesquisa.Text) <> EmptyStr then
    SQL := SQL +
      ' and (forcnpj like "%' + trim(edtPesquisa.Text) + '%" ' +
      '      or forrazaosocial like "%' + trim(edtPesquisa.Text) + '%")';

  SQL :=
    'select *      ' +
    '  from tblfor ' +
    ' where 1 = 1  ' +
    SQL +
    ' order by forrazaosocial';

  dmDadosFornecedores.FiltrarFornecedores(SQL);

end;

procedure TfrmPesquisaCliFor.FormShow(Sender: TObject);
begin
  rgTipoPesquisaClick(rgTipoPesquisa);
end;

procedure TfrmPesquisaCliFor.rgTipoPesquisaClick(Sender: TObject);
begin
  if rgTipoPesquisa.ItemIndex = 0 then // Clientes
  begin
    PageControl.Pages[0].TabVisible := true;  // Tab de consulta de clientes
    PageControl.Pages[1].TabVisible := false; // Tab de consulta de fornecedores
  end
  else
  begin
    PageControl.Pages[0].TabVisible := false;  // Tab de consulta de clientes
    PageControl.Pages[1].TabVisible := true; // Tab de consulta de fornecedores
  end;

  edtPesquisa.SetFocus;

  Filtrar;
end;

end.
